var urlorder = 'http://localhost:8080/service/Order.groovy';
var $j = jQuery.noConflict();

/*$j(document).ready(function () {
	getAddressList();
});*/

addAddr = function() {
	
	var tbody = document.getElementById("addrTbodyDiv");
	var addrNumber = tbody.rows.length;
	var addr = document.createElement("tr");
	var addr_line1 = document.createElement("tr");
	var addr_line2 = document.createElement("tr");
	var country_id = document.createElement("tr");
	var state_id = document.createElement("tr");
	var city = document.createElement("tr");
	var zip_code = document.createElement("tr");
	var phone_number = document.createElement("tr");
	
	addr.innerHTML = '<div><table><tbody id="eachAddrTbody"><tr><td><span id="spanFullName">full name: </td><td><input id="inputFullName"/></td><td><span style="visibility: hidden" id="msgErrorFullName">* Sorry, your full name must be between 1 and 80 characters long.</span></td></tr></tbody></table></div>';
	
	tbody.appendChild(addr);
	var td1 = document.createElement("td");
	var td2 = document.createElement("td");
	var td3 = document.createElement("td");
	
	td1.innerHTML = '<span id="spanAddress_line_1">address: </span>';
	td2.innerHTML = '<input id="inputAddress_line_1"/>';
	td3.innerHTML = '<span style="visibility: hidden" id="msgErrorAddress_line_1">* Sorry, your address must be between 1 and 80 characters long.</span>';
	addr_line1.appendChild(td1);
	addr_line1.appendChild(td2);
	addr_line1.appendChild(td3);
	
	td1 = document.createElement("td");
	td2 = document.createElement("td");
	td3 = document.createElement("td");
	td1.innerHTML = '<span id="spanAddress_line_2">extra information: </span>';
	td2.innerHTML = '<input id="inputAddress_line_2"/>';
	td3.innerHTML = '<span style="visibility: hidden" id="msgErrorAddress_line_2">* Sorry, this field must be between 1 and 80 characters long.</span>';
	addr_line2.appendChild(td1);
	addr_line2.appendChild(td2);
	addr_line2.appendChild(td3);
	
	td1 = document.createElement("td");
	td2 = document.createElement("td");
	td3 = document.createElement("td");
	td2.setAttribute("id", "countryTd")
	td1.innerHTML = '<span id="spanCountry">country: </span>';
	td3.innerHTML = '<span style="visibility: hidden" id="msgErrorCountry">* You must select a country.</span>';
	country_id.appendChild(td1);
	country_id.appendChild(td2);
	country_id.appendChild(td3);
	
	td1 = document.createElement("td");
	td2 = document.createElement("td");
	td3 = document.createElement("td");
	td2.setAttribute("id", "stateTd")
	td1.innerHTML = '<span id="spanState">state: </span>';
	td3.innerHTML = '<span style="visibility: hidden" id="msgErrorState">* You must select a state.</span>';
	state_id.appendChild(td1);
	state_id.appendChild(td2);
	state_id.appendChild(td3);
	
	td1 = document.createElement("td");
	td2 = document.createElement("td");
	td3 = document.createElement("td");
	td1.innerHTML = '<span id="spanCity">city: </span>';
	td2.innerHTML = '<input id="inputCity"/>';
	td3.innerHTML = '<span style="visibility: hidden" id="msgErrorCountry">* Your city must be between 1 and 80 characters long.</span>';
	city.appendChild(td1);
	city.appendChild(td2);
	city.appendChild(td3);
	
	td1 = document.createElement("td");
	td2 = document.createElement("td");
	td3 = document.createElement("td");
	td1.innerHTML = '<span id="spanZipCode">zip code: </span>';
	td2.innerHTML = '<input id="inputZip_code"/>';
	td3.innerHTML = '<span style="visibility: hidden" id="msgErrorZipCode">* You must complete a zip code, it must be between 1 and 8 characters long.</span>';
	zip_code.appendChild(td1);
	zip_code.appendChild(td2);
	zip_code.appendChild(td3);
	
	td1 = document.createElement("td");
	td2 = document.createElement("td");
	td3 = document.createElement("td");
	td1.innerHTML = '<span id="spanPhone">phone number: </span>';
	td2.innerHTML = '<input id="inputPhone_number"/>';
	td3.innerHTML = '<span style="visibility: hidden" id="msgErrorPhone">* You must complete a phone number, it must be between 1 and 25 characters long.</span>';
	phone_number.appendChild(td1);
	phone_number.appendChild(td2);
	phone_number.appendChild(td3);
	
	var eachAddrTbody = document.getElementById('eachAddrTbody');
	eachAddrTbody.appendChild(addr_line1);
	eachAddrTbody.appendChild(addr_line2);
	eachAddrTbody.appendChild(country_id);
	eachAddrTbody.appendChild(state_id);
	eachAddrTbody.appendChild(city);
	eachAddrTbody.appendChild(zip_code);
	eachAddrTbody.appendChild(phone_number);
	
	addCountryList(tbody);
	var button = document.getElementById('addAddressId');
	button.setAttribute("value", "add");
	button.setAttribute("onclick", "submitAddress();");
};

addCountryList = function(node) {
	
	var tbody = document.getElementById("addrTbodyDiv");
	var resp;
	var select = document.createElement("select");
	select.setAttribute("name", "country");
	select.setAttribute("id", "inputCountry");
	select.setAttribute("onchange", 'addStateList();');
	var option;

	var req = new Ajax.Request(urlcommon, {
		method: 'post',
		parameters: {method: 'GetCountryList', language_id: '1'},
		onSuccess: function(transport){
			resp = transport.responseXML;
			if(!checkResp(resp)){
				return;
			}
			
			var countryList2 = resp.getElementsByTagName('country');
			var tr;
			
			option = document.createElement('option');
			option.innerHTML = 'select a country';
			option.setAttribute('value', '');
			select.appendChild(option);
			tr = document.getElementById("countryTd");
			tr.appendChild(select);
			
			for(var i=0; i<countryList2.length; i++) {
				option = document.createElement('option');
				var aux = countryList2[i].lastElementChild.childNodes[0].nodeValue;
				option.innerHTML = aux;
				option.setAttribute('value', countryList2[i].getAttribute('id'));
				select.appendChild(option);
				tr = document.getElementById("countryTd");
				tr.appendChild(select);
			}
		},
		onFailure: function(){ Alert('Sorry, we have a little problem with the server, please try later');}
  });
};



addStateList = function() {
	var tbody = document.getElementById("addrTbodyDiv");
	var resp;
	var select = document.createElement("select");
	select.setAttribute("name", "state");
	select.setAttribute("id", "inputState");
	var option;

	var selectedCountryId = document.getElementById("countryTd").firstChild.value;
	
	var req = new Ajax.Request(urlcommon, {
		method: 'post',
		parameters: {method: 'GetStateList', language_id: '1', country_id: selectedCountryId},
		onSuccess: function(transport){
			resp = transport.responseXML;
			if(!checkResp(resp)){
				return;
			}
			
			var stateList = resp.getElementsByTagName('state');
			var tr;
			
			option = document.createElement('option');
			option.innerHTML = 'select state';
			option.setAttribute('value', '');
			select.appendChild(option);
			tr = document.getElementById("stateTd");
			
			if(tr.firstChild != null){
				tr.removeChild(tr.firstChild);
			}
			
			tr.appendChild(select);
			
			
			for(var i=0; i<stateList.length; i++) {
				option = document.createElement('option');
				var aux = stateList[i].lastElementChild.childNodes[0].nodeValue;
				option.innerHTML = aux;
				option.setAttribute('value', stateList[i].getAttribute('id'));
				select.appendChild(option);
				tr = document.getElementById("stateTd");
				tr.appendChild(select);
			}
		},
		onFailure: function(){ Alert('Sorry, we have a little problem with the server, please try later');}
  });
	
};

submitAddress = function() {
	inputFullName = $j("#inputFullName");
	inputAddress_line_1 = $j("#inputAddress_line_1");
	inputAddress_line_2 = $j("#inputAddress_line_2");
	inputCountry = $j("#inputCountry");
	inputState = $j("#inputState");
	inputCity = $j("#inputCity");
	inputZipCode = $j("#inputZip_code");
	inputPhone_number = $j("#inputPhone_number");

	spanFullName = $j("#spanFullName");
	spanAddress_line_1 = $j("#spanAddress_line_1");
	spanAddress_line_2 = $j("#spanAddress_line_2");
	spanCountry = $j("#spanCountry");
	spanState = $j("#spanState");
	spanCity = $j("#spanCity");
	spanZipCode = $j("#spanZipCode");
	spanPhone = $j("#spanPhone");

	msgErrorFullName = $j("#msgErrorFullName");
	msgErrorAddress_line_1 = $j("#msgErrorAddress_line_1");
	msgErrorAddress_line_2 = $j("#msgErrorAddress_line_2");
	msgErrorCountry = $j("#msgErrorCountry");
	msgErrorState = $j("#msgErrorState");
	msgErrorCity = $j("#msgErrorCity");
	msgErrorZipCode = $j("#msgErrorZipCode");
	msgErrorPhone = $j("#msgErrorPhone");
	
	inputFullName.blur(validateFN);
	inputAddress_line_1.blur(validateLine1);
	//inputAddress_line_2.blur = validate(inputAddress_line_2, spanAddress_line_2, msgErrorAddress_line_2, 80);
	inputCountry.blur(validateCountry);
	inputState.blur(validateState);
	inputCity.blur(validateCity);
	inputZipCode.blur(validateZipCode);
	inputPhone_number.blur(validatePhone_number);
	
	inputFullName.keyup(validateFN);
	inputAddress_line_1.keyup(validateLine1);
	//inputAddress_line_2.blur = validate(inputAddress_line_2, spanAddress_line_2, msgErrorAddress_line_2, 80);
	inputCountry.keyup(validateCountry);
	inputState.keyup(validateState);
	inputCity.keyup(validateCity);
	inputZipCode.keyup(validateZipCode);
	inputPhone_number.keyup(validatePhone_number);

	if(validate(inputFullName, spanFullName, msgErrorFullName, 80) 
			&& validate(inputAddress_line_1, spanAddress_line_1, msgErrorAddress_line_1, 80)
			&& validateId(inputCountry, spanCountry, msgErrorCountry)
			&& validateId(inputState, spanState, msgErrorState)
			&& validate(inputCity, spanCity, msgErrorCity, 80)
			//&& validate(inputZipCode, spanZipCode, msgErrorZipCode, 8)
			&& validate(inputPhone_number, spanPhone, msgErrorPhone, 25)){
				
		var addrXml = '<address><full_name>'+inputFullName.val()+'</full_name><address_line_1>'+inputAddress_line_1.val()+'</address_line_1><address_line_2>'+inputAddress_line_2.val()+'</address_line_2><country_id>'+inputCountry.attr("value")+'</country_id><state_id>'+inputState.attr("value")+'</state_id>'+'<city>'+inputCity.val()+'</city><zip_code>'+inputZipCode.val()+'</zip_code><phone_number>'+inputPhone_number.val()+'</phone_number></address>'; 
		
		var req = new Ajax.Request(urlorder, {
			method: 'post',
			parameters: {method: 'CreateAddress', username: currentUsr, authentication_token: currentToken, address: addrXml},
			onSuccess: function(transport){
				resp = transport.responseXML;

				if(checkResp(resp)) {
					displayAlert('Address added!');
					// mover hacia una tabla o inhibir el contenido ya aniadido, invocar changeButton
					//getAddressList();
					changeButton();
					clearTable();
				}	
			},
			
			onFailure: function(){ alert('Sorry, we have a little problem with the server, please try later')}
		});
	}
	
};

saveAddress = function(index){
	inputFullName = $j('#'+'inputFull_name'+index);
	inputAddress_line_1 = $j('#'+'inputAddress_line_1'+index);
	inputAddress_line_2 = $j('#'+'inputAddress_line_2'+index);
	inputCountry = $j('#'+'inputCountry'+index);
	inputState = $j('#'+'inputState'+index);
	inputCity = $j('#'+'inputCity'+index);
	inputZipCode = $j('#'+'inputZip_code'+index);
	inputPhone_number = $j('#'+'inputPhone_number'+index);
	
	inputAddr_id = $j('#'+'address_id'+index);
	//var addr_id = document.getElementById('address_id'+index).firstChild[0].nodeValue;
	
			
	var addrXml = '<address id="'+inputAddr_id.text()+'"><full_name>'+inputFullName.val()+'</full_name><address_line_1>'+inputAddress_line_1.val()+'</address_line_1><address_line_2>'+inputAddress_line_2.val()+'</address_line_2><country_id>'+inputCountry.attr("value")+'</country_id><state_id>'+inputState.attr("value")+'</state_id>'+'<city>'+inputCity.val()+'</city><zip_code>'+inputZipCode.val()+'</zip_code><phone_number>'+inputPhone_number.val()+'</phone_number></address>'; 
	
	var req = new Ajax.Request(urlorder, {
		method: 'post',
		parameters: {method: 'UpdateAddress', username: currentUsr, authentication_token: currentToken, address: addrXml},
		onSuccess: function(transport){
			resp = transport.responseXML;

			if(checkResp(resp)) {
				document.location.href ='editAddress.html';
				displayPopUp('','Address updated!');
				// mover hacia una tabla o inhibir el contenido ya aniadido, invocar changeButton
				//getAddressList();
				//changeButton();
				//clearTable();
				
			}
			
		},
		
		onFailure: function(){ alert('Sorry, we have a little problem with the server, please try later')}
	});
};

changeButton = function() {
	var button = document.getElementById("addAddressId");
	button.setAttribute("value", "add new address");
	button.setAttribute("onclick", "addAddr();");
	button = document.getElementById("resetAddressId");
	button.setAttribute("value", "cancel");
	button.setAttribute("onclick", "cancelAddAddr();");
	
};

cancelAddAddr = function() {
	document.location.href = "home.html";
};

clearTable = function() {
	var tbody = document.getElementById('eachAddrTbody');
	var table = tbody.parentNode;
	var div = table.parentNode;
	div.removeChild(table);
};



